<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->       
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
System.out.println("car_name:"+request.getParameter("car_name"));
System.out.println(request.getParameter("license_plate"));
PreparedStatement pstmt  =null;
Connection conn=null;
ResultSet rs = null;
try{
	Class.forName("org.h2.Driver");//加载驱动
} catch (ClassNotFoundException e) {
	e.printStackTrace();
	return;
}
try {
	conn = DriverManager.getConnection("jdbc:h2:d:/CompanyCar/temp/mydbtest", "sa", "");
	String sql1="SELECT * FROM car_info WHERE license_plate = ?;";
	pstmt=conn.prepareStatement(sql1);
	pstmt.setString(1,request.getParameter("license_plate"));
	System.out.println(sql1);
	rs = pstmt.executeQuery();
    if (!rs.next()) 
    {
        // 如果查询结果为空，则弹出提示框
        out.println("<script>alert('该车牌号不存在！');window.history.back();</script>");
    }
    else
    {
    	String sql="insert into car_repair(license_plate,car_name,repair_type,repair_cost,repair_reason,repair_person,transport_department_person,repair_status) values(?,?,?,?,?,?,?,?)";
    	pstmt  = conn.prepareStatement(sql);
    	pstmt.setString(1,request.getParameter("license_plate"));
    	pstmt.setString(2,request.getParameter("car_name"));
    	String repair_type=request.getParameter("repair_type1");
    	if(request.getParameter("repair_type2")!=null&&!request.getParameter("repair_type2").isEmpty())
    		repair_type=repair_type+"、"+request.getParameter("repair_type2");
    	if(request.getParameter("repair_type3")!=null&&!request.getParameter("repair_type3").isEmpty())
    		repair_type=repair_type+"、"+request.getParameter("repair_type4");
    	if(request.getParameter("repair_type4")!=null&&!request.getParameter("repair_type4").isEmpty())
    		repair_type=repair_type+"、"+request.getParameter("repair_type4");
    	pstmt.setString(3,repair_type);
    	Float cost=Float.parseFloat(request.getParameter("repair_cost1"));
    	if(request.getParameter("repair_cost2")!=null&&!request.getParameter("repair_cost2").isEmpty())
    		cost+=Float.parseFloat(request.getParameter("repair_cost2"));
    	if(request.getParameter("repair_cost3")!=null&&!request.getParameter("repair_cost3").isEmpty())
    		cost+=Float.parseFloat(request.getParameter("repair_cost3"));
    	if(request.getParameter("repair_cost4")!=null&&!request.getParameter("repair_cost4").isEmpty())
    		cost+=Float.parseFloat(request.getParameter("repair_cost4"));
    	pstmt.setFloat(4,cost);
    	String repair_reason=request.getParameter("repair_reason1");
    	if(request.getParameter("repair_reason2")!=null&&!request.getParameter("repair_reason2").isEmpty())
    		repair_reason=repair_reason+"、"+request.getParameter("repair_reason2");
    	if(request.getParameter("repair_reason3")!=null&&!request.getParameter("repair_reason3").isEmpty())
    		repair_reason=repair_reason+"、"+request.getParameter("repair_reason3");
    	if(request.getParameter("repair_reason4")!=null&&!request.getParameter("repair_reason4").isEmpty())
    		repair_reason=repair_reason+"、"+request.getParameter("repair_reason4");
    	pstmt.setString(5,repair_reason);
    	pstmt.setString(6,request.getParameter("applicant"));
    	pstmt.setString(7,request.getParameter("transportation-manager"));
    	pstmt.setString(8,"已维修");
    	System.out.println(sql);
    	//执行数据库插入，获取结果集
    	int result=pstmt.executeUpdate();
    	System.out.println(result);
    	response.sendRedirect("D-add_repair.jsp");
    }
	
	rs.close();
	//关闭表达式对象
	pstmt.close();			
	//关闭数据库				
	conn.close();
} catch (SQLException e) {
	e.printStackTrace();
} finally{
//防止数据库未正常关闭
try{
	if (conn!=null && (!conn.isClosed())){
		conn.close();
	}
}catch(SQLException e){
	e.printStackTrace();
}
}

%>


</body>
</html>